{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b45c8e23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "Name: Model\n",
      "\n",
      "所以你说你在研究 FPV 无人机？\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "Name: Ronnie\n",
      "\n",
      "是的，没错。\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "Name: Model\n",
      "\n",
      "太好了，你想了解什么？\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "Name: Ronnie\n",
      "\n",
      "我想了解如何驾驶 FPV 无人机。\n"
     ]
    }
   ],
   "source": [
    "from pprint import pprint\n",
    "from langchain_core.messages import AIMessage, HumanMessage\n",
    "\n",
    "messages = [AIMessage(content=f\"所以你说你在研究 FPV 无人机？\", name=\"Model\")]\n",
    "messages.append(HumanMessage(content=f\"是的，没错。\",name=\"Ronnie\"))\n",
    "messages.append(AIMessage(content=f\"太好了，你想了解什么？\", name=\"Model\"))\n",
    "messages.append(HumanMessage(content=f\"我想了解如何驾驶 FPV 无人机。\", name=\"Ronnie\"))\n",
    "\n",
    "for m in messages:\n",
    "    m.pretty_print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e42e25ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, getpass\n",
    "from dotenv import load_dotenv,find_dotenv\n",
    "\n",
    "load_dotenv(find_dotenv())  # 加载 .env 文件中的环境变量\n",
    "\n",
    "api_key = os.environ.get('OPENAI_API_KEY')\n",
    "base_url = os.environ.get('OPENAI_BASE_URL')\n",
    "\n",
    "def _set_env(var: str):\n",
    "    if not os.environ.get(var):\n",
    "        os.environ[var] = getpass.getpass(f\"{var}: \")\n",
    "\n",
    "_set_env(\"OPENAI_API_KEY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7388a089",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "langchain_core.messages.ai.AIMessage"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "llm = ChatOpenAI(model=\"gpt-4o\",\n",
    "                timeout=60 )\n",
    "result = llm.invoke(messages)\n",
    "type(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "76afdaef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='驾驶FPV（第一人称视角）无人机是一项令人兴奋的活动，但需要一定的技术和设备准备。以下是一些步骤和建议，帮助你入门：\\n\\n1. **了解设备：**\\n   - **FPV无人机:** 选择一款适合初学者的FPV无人机，通常自带摄像头。\\n   - **遥控器:** 你将需要一个遥控器来控制无人机，这通常是一个标准双摇杆控制器。\\n   - **FPV护目镜或显示屏:** 戴上FPV护目镜，你能通过无人机上的摄像头实时观看飞行画面。\\n   - **电池和充电器:** 确保有足够的电池和一个可靠的充电器，以便延长飞行时间。\\n\\n2. **法规与安全：**\\n   - 熟悉你所在国家或地区的无人机飞行法规，包括空域限制和许可要求。\\n   - 选择空旷、安全的地方进行飞行，远离人群、建筑物和禁飞区。\\n\\n3. **飞行前准备：**\\n   - 学习如何进行无人机和遥控器的基础设置、校准和检查。\\n   - 进行飞行前的安全检查，确保所有部件正常工作。\\n\\n4. **基础操控练习：**\\n   - 从基础飞行练习开始，例如升降、转向、悬停。\\n   - 逐渐提高难度，练习更复杂的机动，比如直线飞行、绕圈、8字飞行。\\n\\n5. **模拟器练习：**\\n   - 使用FPV飞行模拟器进行练习，可以帮助你在不损失真实无人机的情况下提升操控能力。\\n\\n6. **飞行技巧：**\\n   - 学习和掌握不同的飞行模式，如稳定模式（适合初学者）、运动模式（自由度更高）、和全手动模式（适合高级飞手）。\\n   - 尝试不同的地形和障碍，逐步挑战自己。\\n\\n7. **社区学习：**\\n   - 加入FPV飞行社区和论坛，与其他爱好者交流，分享经验和建议。\\n   - 观看在线教程和比赛视频，学习不同技巧和风格。\\n\\n通过以上步骤，你将逐渐掌握FPV无人机飞行的乐趣和技巧。安全第一，享受飞行！', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 526, 'prompt_tokens': 65, 'total_tokens': 591, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_ee1d74bde0', 'id': 'chatcmpl-BP65qK7ciotA1FjNmiG66MSNF5Vty', 'finish_reason': 'stop', 'logprobs': None}, id='run-2ab8e490-208f-4981-aaa0-92f4809f5fc5-0', usage_metadata={'input_tokens': 65, 'output_tokens': 526, 'total_tokens': 591, 'input_token_details': {}, 'output_token_details': {}})"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c725baca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'token_usage': {'completion_tokens': 526,\n",
       "  'prompt_tokens': 65,\n",
       "  'total_tokens': 591,\n",
       "  'completion_tokens_details': None,\n",
       "  'prompt_tokens_details': None},\n",
       " 'model_name': 'gpt-4o-2024-08-06',\n",
       " 'system_fingerprint': 'fp_ee1d74bde0',\n",
       " 'id': 'chatcmpl-BP65qK7ciotA1FjNmiG66MSNF5Vty',\n",
       " 'finish_reason': 'stop',\n",
       " 'logprobs': None}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.response_metadata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f73bf237",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10",
   "language": "python",
   "name": "python310"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
